<template>
	<div class="container">
		<div class="headerTitle">
			<div class="hSpan">
				<span class="ico"></span>
				{{ $t('message.homeTableTitle') }}
			</div>
			<div class="hSpan refreshText">
				<span class="freushico"></span>
				{{ $t('message.refresh') }}
			</div>
		</div>
		<TableCell :list="tableList" :scroll="{x:1900,y:'auto'}" :hhgPagination="true" :displaySearhList="displaySearhList" :columns="columns" :pageSize="pager.pageSize" :total="total" @getPageInfo="getPageInfo" @clearFilter="clearFilter" :loading="loading"/>
	</div>
</template>
<script setup>
import { onMounted, ref, provide, onUnmounted } from 'vue'
import { TableColumns } from "@/utils/tableConfig"
import { list } from '@/api/data'

import TableCell from '@/components/hhg-table/hhg-table'

//页码的相关
const total = ref(null)
const pager = ref({
	current: 1,
	pageSize: 10,
	pageSizeOptions: [
		{
			value: 10,
			label: '10',
		},
		{
			value: 20,
			label: '20',
		},
		{
			value: 30,
			label: '30',
		},
		{
			value: 50,
			label: '50',
		}
	]
})
//回显搜索框
const displaySearhList = ref({
	companyName: '华宏',
	title: 'title',
	appCode: '我只是测试申请编号'
})


//提供数据给页码组件
provide('pager', pager)
//表头
const columns =  ref(TableColumns.HomeColumns)
//表格数据
const tableList = ref([])
//接口请求的方法
const getList = async (current, pageSize) => {
	const res = await list({
		current: current ? current : pager.value.current,
		pageSize: pageSize ? pageSize : pager.value.pageSize
	})
	if (res.code) {
		tableList.value = res.data.list
		total.value = res.data.total
	}
}
//搜索方法-回车-filter-排序
const getPageInfo = async params=> {
	const res = await list({
		...params
	})
	if (res.code) {
		
		tableList.value = res.data.list
		total.value = res.data.total
	}
}
//清空搜索
const clearFilter = async params=> {
	const res = await list({
		...params
	})
	if (res.code) {
		
		tableList.value = res.data.list
		total.value = res.data.total
	}
}
//监听关闭子窗口的回调
const closeWindow = value=> {
	
	if (value.data === 'close') {
		getList()
	}
}

// mounted
onMounted(() => {
	getList()
	window.addEventListener('message', closeWindow)
})
// 移除消息监听器，防止内存泄漏
onUnmounted(() => {
  window.removeEventListener('message', closeWindow)
})
</script>
<style scoped lang="scss"></style>